package com.hyf.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hyf.commonutils.JwtUtils;
import com.hyf.commonutils.fruit;
import com.hyf.order.entity.Order;
import com.hyf.order.service.OrderService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author hyf
 * @since 2022-05-08
 */
@CrossOrigin// 跨域
@RestController
@RequestMapping("/order/order")
public class OrderController {

	@Autowired
	private OrderService orderService;// 订单服务
	
	// 生成订单的方法
	@PostMapping("createOrder/{courseId}")
	public fruit saveOrder(@PathVariable String courseId, HttpServletRequest request) {
		// 创建订单，返回订单号
	String ordersprderNo= orderService.createOrders(courseId, JwtUtils.getMemberIdByJwtToken(request));
	
		
		return fruit.ok().data("orderId", ordersprderNo);
	}
	
	
	// 根据订单id 查询订单信息
	@GetMapping("/getOrderInfo/{orderId}")
	public fruit getOrder(@PathVariable String orderId) {
		QueryWrapper<Order> wrapper = new QueryWrapper<>();
		wrapper.eq("order_no", orderId);
		 Order one = orderService.getOne(wrapper);
		return fruit.ok().data("order",one);
	}
	
	
	// 查询订单支付状态，如果支付成功，显示立即观看0、1
	// 根据课程id和用户id查询订单的状态
	@GetMapping("isBuyCourse/{memberId}/{coureId}")
	public boolean isBuyCourse(@PathVariable String memberId,
							   @PathVariable String coureId) {
		//订单状态是1表示支付成功
	/*	int count = orderService.count(new QueryWrapper<Order>().eq("member_id", memberid).eq("course_id", id).eq("status", 1));
		if(count>0) {
			return true;
			
		} else {
			return false;
		}*/
		QueryWrapper<Order> wrapper = new QueryWrapper<>();
		System.out.println("mem:"+memberId);
		System.out.println("coureId:"+coureId);
		wrapper.eq("member_id",memberId);
		wrapper.eq("course_id",coureId);
		wrapper.eq("status",1);//支付状态
		int count = orderService.count(wrapper);
		// 判断是否支付成功
		if(count>0) {
			return true;
		} else {
			return false;
		}
	
	}
	
}

